﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.NewPBAuto.Presenter;
using MINERP.NewPBAuto.IView;

namespace MINERP.NewPBAuto.Forms
{
    public partial class FAutoPB : Form, IFAutoPBView
    {
        FAtuoPBPresenter _presenter;
        public FAutoPB()
        {
            InitializeComponent();
            _presenter = new FAtuoPBPresenter(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (btnOk.Text == "开始")
            {
                lblStatus.Text = "正在准备自动拼板...";
                _presenter.StartFAutoPB();
            }
            else if (btnOk.Text == "暂停")
            {
                _presenter.StopFAutoPB();
            }
        }

        #region IFAutoPBView 成员

        public float CtmRaste
        {
            get { return Convert.ToSingle(string.IsNullOrEmpty(txtWrate.Text.Trim()) ? "0" : txtWrate.Text.Trim()) / 100; }
        }

        delegate void UpCtrlDelete(string strResult, string strStatus);

        public void UpCtrl(string strResult, string strStatus)
        {
            if (lblStatus.InvokeRequired)
            {
                this.Invoke(new UpCtrlDelete(UpCtrl), new object[] { strResult, strStatus });
                return;
            }
            else
            {      
                lblStatus.Text = strStatus;
                lblOverPB.Text = strResult;
            }
        }

        public void UpCtrlStatus(string strCurBStatus)
        {
            if (listBoxControl1.InvokeRequired)
            {
                this.Invoke(new UpString(UpCtrlStatus), new object[] { strCurBStatus });
                return;
            }
            listBoxControl1.Items.Insert(0,strCurBStatus+"-----------"+DateTime.Now.ToString());
        }

        public void UpCtrlClearBoxCount(string strCount)
        {
            if (lblClearBoxCount.InvokeRequired)
            {
                this.Invoke(new UpString(UpCtrlClearBoxCount), new object[] { strCount });
                return;
            }
            lblClearBoxCount.Text = strCount;
        }

        public void UpCtrlRemainPs(string strCount)
        {
            if (lblCount.InvokeRequired)
            {
                this.Invoke(new UpString(UpCtrlRemainPs), new object[] { strCount });
                return;
            }
            lblCount.Text = strCount;
        }

        delegate void UpString(string msg);
        delegate void UpBtn(string msg,bool btnEnable);
        public void SetBtn(string p, bool btnEnable)
        {
            if (btnOk.InvokeRequired)
            {
                this.Invoke(new UpBtn(SetBtn), new object[] { p,btnEnable });
                return;
            }
            btnOk.Text = p;
            btnOk.Enabled = btnEnable;
            if (p == "开始")
            {
                timer1.Enabled = false;
                progressBar1.Value = 0;
            }
            else if (p == "暂停")
            {
                timer1.Enabled = true;
            }
        }



        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value++;
            if (progressBar1.Value >= progressBar1.Maximum)
            {
                progressBar1.Value = 1;
            }
        }

        private void FAutoPB_FormClosing(object sender, FormClosingEventArgs e)
        {
         
            e.Cancel = !_presenter.CloseConfirm();
        }




        #region IFAutoPBView 成员


        public void CloseFrm()
        {
            this.Close();
        }

        #endregion
    }
}
